package com.bnlr.lrparking.appui.login;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.bnlr.lrparking.app.Application;
import com.bnlr.lrparking.R;
import com.bnlr.lrparking.base.BaseActivity;
import com.bnlr.lrparking.app.AppConfig;
import com.bnlr.lrparking.constant.LrConstant;
import com.bnlr.lrparking.constant.Urls;
import com.bnlr.lrparking.model.VerifyCodeM;
import com.bnlr.lrparking.util.CountDownTimerUtils;
import com.bnlr.lrparking.util.IsPhoneFormatUtils;
import com.bnlr.lrparking.util.CommonUtil;
import com.bnlr.lrparking.view.ClearEditText;
import com.bnlr.nohttp.CallServer;
import com.bnlr.nohttp.CustomHttpListener;
import com.yolanda.nohttp.NoHttp;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/6/6.
 * 找回密码
 */

public class ForgetPassWordActivity extends BaseActivity {
    @BindView(R.id.tv_nav_title)
    TextView tvNavTitle;
    @BindView(R.id.tv_nav_right)
    TextView tvNavRight;
    @BindView(R.id.iv_nav_right)
    ImageView ivNavRight;
    @BindView(R.id.iv_nav_back)
    ImageView ivNavBack;
    @BindView(R.id.et_forget_phone)
    ClearEditText etForgetPhone;
    @BindView(R.id.tv_forget_yzm)
    TextView tvForgetYzm;
    @BindView(R.id.et_forget_yzm)
    ClearEditText etForgetYzm;
    @BindView(R.id.et_forget_newpsw)
    ClearEditText etForgetNewpsw;
    @BindView(R.id.et_forget_oldpsw)
    ClearEditText etForgetOldpsw;
    @BindView(R.id.btn_findPwd)
    Button btnFindPwd;
    @BindView(R.id.newpsd_close)
    RelativeLayout newpsdClose;
    @BindView(R.id.surepsd_close)
    RelativeLayout surepsdClose;
    @BindView(R.id.img_surepsd)
    ImageView Imgsurepsd;
    @BindView(R.id.img_newpsd)
    ImageView Imgnewpsd;
    private RelativeLayout rely_complain;
    private String mobile = "", password = "", surepsd = "";
    int flag_new = 1;
    int flag_sure = 1;
    String yzm = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forgetpwd);
        ButterKnife.bind(this);
        tvNavTitle.setText("找回密码");
        rely_complain = (RelativeLayout) findViewById(R.id.rely_complain);
        etForgetPhone.addTextChangedListener(mTextWatcher);//手机号监听
        etForgetYzm.addTextChangedListener(textWatcher1);//验证码监听
        etForgetNewpsw.addTextChangedListener(textWatcher1);//新密码监听
        etForgetOldpsw.addTextChangedListener(textWatcher1);//确认密码监听
        rely_complain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(ComplainActivity.class);
            }
        });
        btnFindPwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (!password.equals(surepsd)) {
                    ToastUtils.showShort("请检查输入的密码是否一致");
                } else if (TextUtils.isEmpty(mobile)) {
                    ToastUtils.showShort("手机号不能为空");
                } else {
                    Change();
                }
            }
        });
    }

    // 监听
    private TextWatcher mTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void afterTextChanged(Editable editable) {
            mobile = etForgetPhone.getText().toString();
            // 获取验证码
            if (IsPhoneFormatUtils.isMobileNO(mobile) && mobile.length() == 11) {
                tvForgetYzm.setBackgroundResource(R.mipmap.verificationbox_click);
                tvForgetYzm.setEnabled(true);
                tvForgetYzm.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        SendYzm();
                    }
                });
            } else {
                tvForgetYzm.setEnabled(false);
                tvForgetYzm.setBackgroundResource(R.mipmap.verificationbox_dis);
            }
        }
    };

    private TextWatcher textWatcher1 = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void afterTextChanged(Editable editable) {
            password = etForgetNewpsw.getText().toString();
            surepsd = etForgetOldpsw.getText().toString();
            yzm = etForgetYzm.getText().toString();
            //重置密码按钮
            if (!TextUtils.isEmpty(yzm) && (password.length() >= 6 && password.length() <= 16)
                    && !TextUtils.isEmpty(surepsd)) {
                btnFindPwd.setEnabled(true);
            } else {
                btnFindPwd.setEnabled(false);
            }

        }
    };


    @Override
    public void doClick(View v) {
        super.doClick(v);
        switch (v.getId()) {
            //
            case R.id.newpsd_close:
                if (flag_new == 1) {
                    flag_new = 2;
                    Imgnewpsd.setImageResource(R.mipmap.login_icon05);
                    etForgetNewpsw.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    flag_new = 1;
                    Imgnewpsd.setImageResource(R.mipmap.login_icon04);
                    etForgetNewpsw.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                break;
            //
            case R.id.surepsd_close:
                if (flag_sure == 1) {
                    flag_sure = 2;
                    Imgsurepsd.setImageResource(R.mipmap.login_icon05);
                    etForgetOldpsw.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    flag_sure = 1;
                    Imgsurepsd.setImageResource(R.mipmap.login_icon04);
                    etForgetOldpsw.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                break;
        }
    }


    /**
     * 获取验证码
     */
    private void SendYzm() {
        try {
            mRequest = NoHttp.createStringRequest(Urls.IP, LrConstant.POST);
            mRequest.add("action", "sendValidateMCD");
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("phone", mobile);
            mRequest.add("flag", "1");
            mRequest.add("uid", AppConfig.getInstance().getString("uid", ""));
            // 添加到请求队列
            CallServer.getRequestInstance().add(baseContext, mRequest,
                    new CustomHttpListener<VerifyCodeM>(baseContext, true, VerifyCodeM.class) {
                        @Override
                        public void doWork(VerifyCodeM data, String code) {
                        }

                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {
                            try {
                                super.onFinally(obj, code, isSucceed);
                                if ("1".equals(code)) {
                                    setVerifica(tvForgetYzm);
                                } else {
                                    CommonUtil.showToask(baseContext, obj.getString("msg"));
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }, true);
        } catch (Exception e) {

        }
    }

    private void Change() {
        try {
            mRequest = NoHttp.createStringRequest(Urls.IP, LrConstant.POST);
            mRequest.add("action", "GetPwdByUserName");
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("phone", mobile);
            mRequest.add("pwd", password);
            mRequest.add("code", yzm);
            mRequest.add("uid", AppConfig.getInstance().getString("uid", ""));
            // 添加到请求队列
            CallServer.getRequestInstance().add(baseContext, mRequest,
                    new CustomHttpListener<JSONObject>(baseContext, false, null) {
                        @Override
                        public void doWork(JSONObject data, String code) {
                        }

                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {
                            try {
                                super.onFinally(obj, code, isSucceed);
                                if ("1".equals(code)) {
                                    ToastUtils.showShort("找回密码成功");
                                    ForgetPassWordActivity.this.finish();
                                } else {
                                    ToastUtils.showShort(obj.getString("msg"));
                                }

                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }, true);
        } catch (Exception e) {
        }
    }

    private void setVerifica(TextView verifica) {
        CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(verifica, 60000, 1000);
        mCountDownTimerUtils.start();
    }
}
